package demo.orm;

import demo.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : GuoFei
 * @date : 2019/1/22 10:58
 */
public class TableContext {

    private static Logger log = LoggerFactory.getLogger(TableContext.class);

    public static Map<String, TableInfo> tables = new HashMap<>();

    private TableContext() {
    }

    public static TableInfo poClassTableMap(Object obj){
        String key = StringUtils.firstToLowCase(obj.getClass().getSimpleName());
        return tables.get(key);
    }

    static {
        try {
            Connection con = DBManage.getConn();
            if (con == null) {
                log.error("获取数据库链接成功");
            }else {
                log.info("获取数据库链接成功");
                DatabaseMetaData dbmd = con.getMetaData();
                ResultSet tableRet = dbmd.getTables(null, "%", "%", new String[]{"TABLE"});
                while (tableRet.next()) {
                    String tableName = String.valueOf(tableRet.getObject("TABLE_NAME"));
                    TableInfo ti = new TableInfo();
                    ti.setTableName(tableName);
                    ti.setColumns(new HashMap<>());
                    ti.setPriKeys(new ArrayList<>());
                    tables.put(tableName, ti);
                    ResultSet set = dbmd.getColumns(null, "%", tableName, "%");
                    while (set.next()) {
                        ColumnInfo ci = new ColumnInfo();
                        ci.setName(set.getString("column_name"));
                        ci.setDataType(set.getString("TYPE_NAME"));
                        ci.setKeyType(0);
                        ti.getColumns().put(set.getString("column_name"), ci);
                    }
                    ResultSet set2 = dbmd.getPrimaryKeys(null, "%", tableName);
                    while (set2.next()) {
                        String columnName = set2.getString("column_name");
                        log.info(columnName);
                        ColumnInfo ci2 = ti.getColumns().get(columnName);
                        ci2.setKeyType(1);
                        ti.getPriKeys().add(ci2);
                    }

                        String sql = "SELECT COLUMN_NAME colname FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = '"+tableName+"' AND constraint_name = ( SELECT B.NAME FROM SYSOBJECTS A JOIN SYSOBJECTS B ON A.ID = B.PARENT_OBJ AND A.XTYPE = 'U' AND B.XTYPE = 'PK' WHERE A.NAME = '"+tableName+"' )";
                    ResultSet sqlSet = DBManage.getConn().prepareStatement(sql).executeQuery();
                    if (sqlSet.next()){
                        String idName = sqlSet.getString(1);
                        ColumnInfo columnInfo = new ColumnInfo();
                        columnInfo.setKeyType(0);
                        columnInfo.setDataType("int");
                        columnInfo.setName(idName);
                        ti.setOnlyPriKey(columnInfo);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
